package com.sz.admin.aichatmessage.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.sz.admin.aichatmessage.service.AiChatMessageService;
import com.sz.admin.aichatmessage.pojo.po.AiChatMessage;
import com.sz.admin.aichatmessage.mapper.AiChatMessageMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.query.QueryChain;
import com.sz.core.common.enums.CommonResponseEnum;
import com.sz.core.util.PageUtils;
import com.sz.core.util.BeanCopyUtils;
import com.sz.core.util.Utils;
import com.sz.core.common.entity.PageResult;
import com.sz.core.common.entity.SelectIdsDTO;
import java.io.Serializable;
import java.util.List;
import com.sz.admin.aichatmessage.pojo.dto.AiChatMessageCreateDTO;
import com.sz.admin.aichatmessage.pojo.dto.AiChatMessageUpdateDTO;
import com.sz.admin.aichatmessage.pojo.dto.AiChatMessageListDTO;
import com.sz.admin.aichatmessage.pojo.dto.AiChatMessageImportDTO;
import com.sz.core.common.entity.ImportExcelDTO;
import com.sz.excel.core.ExcelResult;
import java.io.OutputStream;
import jakarta.servlet.http.HttpServletResponse;
import com.sz.core.util.FileUtils;
import com.sz.excel.utils.ExcelUtils;
import lombok.SneakyThrows;
import com.sz.admin.aichatmessage.pojo.vo.AiChatMessageVO;

/**
 * <p>
 * AI对话消息 服务实现类
 * </p>
 *
 * @author sz-admin
 * @since 2025-03-11
 */
@Service
@RequiredArgsConstructor
public class AiChatMessageServiceImpl extends ServiceImpl<AiChatMessageMapper, AiChatMessage> implements AiChatMessageService {
    @Override
    public void create(AiChatMessageCreateDTO dto){
        AiChatMessage aiChatMessage = BeanCopyUtils.copy(dto, AiChatMessage.class);
        save(aiChatMessage);
    }

    @Override
    public void update(AiChatMessageUpdateDTO dto){
        AiChatMessage aiChatMessage = BeanCopyUtils.copy(dto, AiChatMessage.class);
        QueryWrapper wrapper;
        // id有效性校验
        wrapper = QueryWrapper.create()
            .eq(AiChatMessage::getId, dto.getId());
        CommonResponseEnum.INVALID_ID.assertTrue(count(wrapper) <= 0);

        saveOrUpdate(aiChatMessage);
    }

    @Override
    public PageResult<AiChatMessageVO> page(AiChatMessageListDTO dto){
        Page<AiChatMessageVO> page = pageAs(PageUtils.getPage(dto), buildQueryWrapper(dto), AiChatMessageVO.class);
        return PageUtils.getPageResult(page);
    }

    @Override
    public List<AiChatMessageVO> list(AiChatMessageListDTO dto){
        return listAs(buildQueryWrapper(dto), AiChatMessageVO.class);
    }

    @Override
    public void remove(SelectIdsDTO dto){
        CommonResponseEnum.INVALID_ID.assertTrue(dto.getIds().isEmpty());
        removeByIds(dto.getIds());
    }

    @Override
    public AiChatMessageVO detail(Object id){
        AiChatMessage aiChatMessage = getById((Serializable) id);
        CommonResponseEnum.INVALID_ID.assertNull(aiChatMessage);
        return BeanCopyUtils.copy(aiChatMessage, AiChatMessageVO.class);
    }

    @SneakyThrows
    @Override
    public void importExcel(ImportExcelDTO dto) {
        ExcelResult<AiChatMessageImportDTO> excelResult = ExcelUtils.importExcel(dto.getFile().getInputStream(), AiChatMessageImportDTO.class, true);
        List<AiChatMessageImportDTO> list = excelResult.getList();
        List<String> errorList = excelResult.getErrorList();
        String analysis = excelResult.getAnalysis();
        System.out.println(" analysis : " + analysis);
        System.out.println(" isCover : " + dto.getIsCover());
    }

    @SneakyThrows
    @Override
    public void exportExcel(AiChatMessageListDTO dto, HttpServletResponse response) {
        List<AiChatMessageVO> list = list(dto);
        String fileName = "AI对话消息模板";
        OutputStream os = FileUtils.getOutputStream(response, fileName + ".xlsx");
        ExcelUtils.exportExcel(list, "AI对话消息", AiChatMessageVO.class, os);
    }

    private static QueryWrapper buildQueryWrapper(AiChatMessageListDTO dto) {
        QueryWrapper wrapper = QueryWrapper.create().from(AiChatMessage.class);
        if (Utils.isNotNull(dto.getThirdPartyName())) {
            wrapper.like(AiChatMessage::getThirdPartyName, dto.getThirdPartyName());
        }
        if (Utils.isNotNull(dto.getSessionId())) {
            wrapper.eq(AiChatMessage::getSessionId, dto.getSessionId());
        }
        if (Utils.isNotNull(dto.getType())) {
            wrapper.eq(AiChatMessage::getType, dto.getType());
        }
        if (Utils.isNotNull(dto.getContent())) {
            wrapper.eq(AiChatMessage::getContent, dto.getContent());
        }
        if (Utils.isNotNull(dto.getReasoningContent())) {
            wrapper.eq(AiChatMessage::getReasoningContent, dto.getReasoningContent());
        }
        if (Utils.isNotNull(dto.getMedias())) {
            wrapper.eq(AiChatMessage::getMedias, dto.getMedias());
        }
        if (Utils.isNotNull(dto.getPlatformName())) {
            wrapper.like(AiChatMessage::getPlatformName, dto.getPlatformName());
        }
        if (Utils.isNotNull(dto.getUseModel())) {
            wrapper.eq(AiChatMessage::getUseModel, dto.getUseModel());
        }
        if (Utils.isNotNull(dto.getTotalToken())) {
            wrapper.eq(AiChatMessage::getTotalToken, dto.getTotalToken());
        }
        if (Utils.isNotNull(dto.getCreateBy())) {
            wrapper.eq(AiChatMessage::getCreateBy, dto.getCreateBy());
        }
        if (Utils.isNotNull(dto.getUpdateBy())) {
            wrapper.eq(AiChatMessage::getUpdateBy, dto.getUpdateBy());
        }
        return wrapper;
    }
}